// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
mod fs_file;
mod chat;
use crate::chat::chat_tool;
use crate::fs_file::fs_tool;
#[tauri::command]
fn addMes(path: &str,json_data:&str) -> String {
    let mut  result_mes = "none";
    let mut log_data_params = json_data.to_string();
    let json_data = r#json_data;
    chat_tool::exist_file(path);
    let scoped_type = "add";
    let is_img = chat_tool::process_input(&json_data,&scoped_type,path);
    match is_img {
        Ok(success_msg) => {
            log_data_params = success_msg;
            println!("获得去掉base{}",log_data_params);
            
        },
        Err(error_msg) => eprintln!("错误: {}", error_msg),
    };
    let log_params_ge = &log_data_params; 
    match chat_tool::log_data(path,r#log_params_ge) {
        Ok(s)=>{
            println!("成功{}",s);
            result_mes = "OK";
                  }
                  Err(e)=>{
                   println!("失败{}",e)
                  } 
    };

    format!("{}", &result_mes)
}
#[tauri::command]
fn deleterow(path: &str,json_data:&str) -> String {
    let scoped_type: &str = "delete";
    let is_img = chat_tool::process_input(&json_data,&scoped_type,path);
    match is_img {
        Ok(success_msg) => {
            chat_tool::delete_image(path,r#json_data);
        },
        Err(error_msg) => eprintln!("错误: {}", error_msg),
    };
    let mut  result_mes = "none";
   let delete_s =  chat_tool::delete_log_entry(path,r#json_data);
   match  delete_s {
       Ok(s)=>{
 println!("成功{}",s);
 result_mes = "OK";
       }
       Err(e)=>{
        println!("失败{}",e)
       }
   };
    format!("{}",&result_mes)  
}
#[tauri::command]
fn initlist(path: &str) -> Result<chat_tool::DataResponse, String> {
    chat_tool::exist_file(path);
    chat_tool::get_all_data(path)

}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_opener::init())
        .invoke_handler(tauri::generate_handler![addMes,deleterow,initlist])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
